﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace Isuka.Excel
{
    /// <summary>
    /// ワークシートコレクション
    /// </summary>
    public class XWorksheets
        : ExcelObject
        , IEnumerable<XWorksheet>
    {
        readonly XWorkbook workbook;

        internal XWorksheets(XWorkbook workbook, object sheets)
            : base(workbook.Application, sheets)
        {
            Contract.Requires(workbook != null);
            Contract.Requires(sheets != null);

            this.workbook = workbook;
        }

        public XWorksheet this[int index]
        {
            get
            {
                Contract.Requires<IndexOutOfRangeException>(0 <= index, "index");
                VerifyNotDisposed();
                return new XWorksheet(workbook, Object[index + 1]);
            }
        }

        public int Count
        {
            get
            {
                return Object.Count;
            }
        }

        public XWorkbook Workbook
        {
            get { return workbook; }
        }

        #region IEnumerable<XWorksheet> メンバー

        public IEnumerator<XWorksheet> GetEnumerator()
        {
            VerifyNotDisposed();

            foreach (object sheet in Object)
            {
                yield return new XWorksheet(workbook, sheet);
            }
        }
        #endregion

        #region IEnumerable メンバー

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
